package com.chagee.store.operation.server.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chagee.base.utils.*;
import com.chagee.store.operation.api.request.*;
import com.chagee.store.operation.server.model.param.BatchUpdatePenalizeParam;
import com.chagee.store.operation.api.response.StoreWorkOrderDeviceResponse;
import com.chagee.store.operation.api.response.WorkOrderResponse;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.model.param.WorkOrderDetailRequest;
import com.chagee.store.operation.server.model.vo.BatchUpdatePenalizeVO;
import com.chagee.store.operation.server.model.vo.mapVO.WorkOrderMapVo;
import com.chagee.store.operation.server.service.WorkOrderService;
import com.chagee.store.operation.server.model.param.WorkOrderDeviceParam;
import com.chagee.storeManagement.api.event.response.BasePageResponse;
import com.chagee.storeManagement.api.event.response.QueryStoreOperateRecordResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @ClassName WorkOrderController  //类名称
 * @Description: //类描述
 * @Author: Jacky-zhan	//作者
 * @CreateDate: 2024/7/2 17:53	//创建时间
 * @UpdateUser: Jacky-zhan  //变更者
 * @UpdateDate: 2024/7/2 17:53	//更新时间
 * @UpdateRemark: //更新的信息
 * @Version: 1.0    //版本号
 */
@Slf4j
@RestController
@RequestMapping("/workOrder")
@Api("三方打样工单信息")
public class WorkOrderController {

	@Resource
	private WorkOrderService workOrderService;

	//@Value("#{${template.batchUploadPenalizeMoney:{}}}")
	@Value("${template.batchUploadPenalizeMoney}")
	private String batchUploadPenalizeMoneyUrl;

	/**
	 *  数据字典
	 */
	@GetMapping("/dict")
	@ApiOperation("PC端开早打样工单获取数据字典")
	public HttpResponse<WorkOrderMapVo> dict() {
		return HttpResponseUtil.success(workOrderService.dict());
	}

	@PostMapping("/getWorkOrderDevice")
	@ApiOperation("PC端开早打样工单获取工单详情的确认交接信息")
	public HttpResponse<StoreWorkOrderDeviceResponse> getWorkOrderDevice(@RequestBody @Valid WorkOrderDeviceParam workOrderDeviceParam) {
		log.info("#WorkOrderController.getWorkOrderDevice#request{}", workOrderDeviceParam);
		StoreWorkOrderDeviceResponse result = workOrderService.getWorkOrderDevice(workOrderDeviceParam);
		return HttpResponseUtil.success(result);
	}

	@PostMapping("/detail")
	@ApiOperation("PC端开早打样工单获取工单详情的基础工单任务信息")
	public HttpResponse<WorkOrderResponse> queryDetail(@RequestBody @Valid WorkOrderDetailRequest request) {
		log.info("#WorkOrderController.queryDetail#request{}", request);
		return HttpResponseUtil.success(workOrderService.queryDetail(request));
	}

	/**
	 * 分页查询工单表
	 */
	@PostMapping("/query")
	@ApiOperation("PC端开早打样工单分页查询工单表")
	public HttpResponse<PaginationData<WorkOrderResponse>> queryListByPage(@RequestBody PaginationRequest<WorkOrderRequest> request) {
		log.info("#WorkOrderController.queryListByPage#request{}", JSONObject.toJSONString(request));
		return HttpResponseUtil.success(workOrderService.query(request));
	}

	/**
	 * 开早打烊工单导出
	 */
	@PostMapping("/export")
	@ApiOperation("打烊工单导出")
	public void exportWorkOrder(@RequestBody WorkOrderRequest taskListParam,
									   HttpServletRequest request,
									   HttpServletResponse response,
									   ModelMap map) {
		workOrderService.export(taskListParam, request, response, map);
	}

	/**
	 * 扣除金额
	 * @return
	 */
	@PostMapping("/penalizeMoney")
	@ApiOperation("扣除金额")
	public HttpResponse<Boolean> penalizeMoney(@RequestBody OrderPenalizeMoneyRequest request) {
		log.info("store_penalizeMoney_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(workOrderService.penalizeMoney(request));
	}

	/**
	 * 下载模版
	 * @return template
	 */
	@GetMapping("/batchUpdatePenalizeMoneyTemplate")
	public HttpResponse<String> batchUploadPenalizeMoneyUrlTemplate() {
		if (StringUtils.isNotEmpty(batchUploadPenalizeMoneyUrl)) {
			log.info("batchUploadPenalizeMoneyUrl template {}", JSONUtil.toJsonStr(batchUploadPenalizeMoneyUrl));
			return HttpResponseUtil.success(batchUploadPenalizeMoneyUrl);
		}
		return HttpResponseUtil.success(null);
	}

	/**
	 * 批量导入扣罚金额
	 * @param request req
	 * @return result
	 */
	@PostMapping("/batchUpdatePenalizeMoney")
	public HttpResponse<BatchUpdatePenalizeVO> batchUpdatePenalizeMoney(@RequestBody BatchUpdatePenalizeParam request) {
		log.info("store_batchUpdatePenalizeMoney_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return HttpResponseUtil.success(workOrderService.batchUpdatePenalizeMoney(request));
	}

	/**
	 * 处理申述
	 * @return
	 */
	@PostMapping("/handleAppeal")
	@ApiOperation("处理申述")
	public HttpResponse<Boolean> handleAppeal(@RequestBody @Valid StoreHandleAppealRequest request) {
		log.info("store_handleAppeal_request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		return workOrderService.handleAppeal(request);
	}

	/**
	 * 查询操作记录 recordType: 11
	 */
	@PostMapping("/queryOperateRecordPage")
	@ApiOperation("查询操作记录")
	public HttpResponse<BasePageResponse<QueryStoreOperateRecordResponse>> queryOperateRecordPage(@RequestBody @Valid PaginationRequest<WorkOrderRecordRequest> param) {
		log.info("#WorkOrderController.queryOperateRecordPage#request{}", JSONObject.toJSONString(param));
		if (StringUtils.isNotBlank(CommonUtil.validate(param))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(param));
		}
		if (Objects.isNull(param.getPageNum()) || Objects.equals(0, param.getPageNum())) {
			param.setPageNum(1);
		}
		if (Objects.isNull(param.getPageSize()) || Objects.equals(0, param.getPageSize())) {
			param.setPageSize(10);
		}
		return HttpResponseUtil.success(workOrderService.queryOperateRecordPage(param));
	}
}
